﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

namespace EC2011_hk1_BT3_1041061_1041129_1041130
{
        public class Database
    {
        //Thuoc tinh
        //protected string m_connectionString = WebConfigurationManager.ConnectionStrings["connectionString"].ToString();
        //"Server=USER\\SQLEXPRESS;Database=ATM;Trusted_Connection=True;";
        protected SqlConnection connection;
        protected SqlDataAdapter da = null;
        protected SqlCommand command;
        string conString = @"Data Source=.;Initial Catalog=PagingData;Integrated Security=True";    //Phuong thuc
        public void connect()
        {
            try
            {
                connection = new SqlConnection(conString);
                connection.Open();                
            }
            catch (Exception ex)
            {
               
            }
        }
        //Tat ket noi
        public void disconnect()
        {
            try
            {
                connection.Close();
            }
            catch (Exception ex)
            {

                //MessageBox.Show(ex.Message);
            }
        }
        //Lay ra du lieu ra table
        public DataTable executeQuery(String sqlString)
        {
            connect();
            DataSet ds = new DataSet();
            da = new SqlDataAdapter(sqlString, connection);
            da.Fill(ds);
            disconnect();
            return ds.Tables[0];
        }
        //Dung cho cac thao tac insert, delete, update
        public void executeNonQuery(string sqlString)
        {
            connect();
            command = new SqlCommand(sqlString, connection);
            command.ExecuteNonQuery();

            disconnect();
        }
        //Lay 1 gia tri du lieu ra
        public object executeScalar(string sqlString)
        {
            connect();
            command = new SqlCommand(sqlString, connection);
            object o = command.ExecuteScalar();
            disconnect();
            return o;
        }

        //public string ConnectionString
        //{
        //    get
        //    {
        //        return m_connectionString;
        //    }
        //    set
        //    {
        //        m_connectionString = value;
        //    }
        //}
    }
}